FROM golang:1.13-alpine as builder

# Add Maintainer Info
LABEL maintainer="Sam Zhou <sam@mixmedia.com>"

# Set the Current Working Directory inside the container
WORKDIR /app

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
RUN go version \
 && export GO111MODULE=on \
 && export GOPROXY=https://goproxy.io \
 && go mod vendor \
 && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o ipa2s3

######## Start a new stage from scratch #######
FROM alpine:latest  

RUN wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.2/dumb-init_1.2.2_amd64 \
 && chmod +x /usr/local/bin/dumb-init \
 && apk add --update libintl \
 && apk add --no-cache ca-certificates tzdata \
 && apk add --virtual build_deps gettext  \
 && cp /usr/bin/envsubst /usr/local/bin/envsubst \
 && apk del build_deps

WORKDIR /app

# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/ipa2s3 .
COPY --from=builder /app/template ./template
COPY --from=builder /app/webroot/swagger ./webroot/swagger
COPY --from=builder /app/webroot/ui ./webroot/ui
COPY --from=builder /app/config.json .

ENV HOST=0.0.0.0:7990 \
 SERVICE_NAME=ipa2s3 \
 S3_APPKEY= \
 S3_SECRET= \
 S3_BUCKET=s3.test.mixmedia.com \
 S3_REGION=ap-southeast-1 \
 S3_PREFIX=/ipa/demo \
 TEMPLATE_PATH=/app/template/list.html \
 ROOT=/app/webroot \
 TEMP=/tmp \
 CLOUD_FRONT_ENABLE=true \
 CLOUD_FRONT_HOST=https://d3oh79m8qt95i6.cloudfront.net \
 OSS_APPKEY= \
 OSS_SECRET= \
 OSS_BUCKET=oss-mixmedia-com \
 OSS_ENDPOINT=oss-cn-shenzhen.aliyuncs.com \
 OSS_PREFIX=temp \
 EMAIL_FROM_NAME=IPA2S3 \
 EMAIL_FROM_EMAIL=no-reply@edm.mixmedia.com \
 SENDGRID_API_KEY= \
 AUTH_ENABLE=true \
 WECHAT_WORK_APPID= \
 WECHAT_WORK_AGENTID= \
 WECHAT_WORK_SECRET=
 
EXPOSE 7990

ENTRYPOINT ["dumb-init", "--"]

CMD envsubst < /app/config.json > /app/temp.json \
 && /app/ipa2s3 -b -c /app/temp.json
